//
//  MoneyDetailVC.m
//  QoilPublic
//
//  Created by SunChao on 2018/2/2.
//  Copyright © 2018年 孙超. All rights reserved.
//

#import "MoneyDetailVC.h"
#import "MoneyDetailCell.h"
@interface MoneyDetailVC ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tbView;
@property (nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic,assign)int index;
@end

@implementation MoneyDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"交易明细";
    [self.view  addSubview:self.tbView];
    [self.tbView registerNib:[UINib nibWithNibName:@"MoneyDetailCell" bundle:nil] forCellReuseIdentifier:@"MoneyDetailCell"];
    self.tbView.tableFooterView = [UIView new];
    [self requestData];
    _index = 1;
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
}


//加载更多
- (void)requestMoreData{
    _index++;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"userinfo/getUserAccount"];
     NSDictionary *param = [QParamService GainTransactionDetailsWithStart:star pageSize:@"5"];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            
            
            
            [self.dataSource addObjectsFromArray:basModel.items];
            
            if(self.dataSource.count < 10){
                
               [_tbView.mj_footer noticeNoMoreData];
                
            }
                
                [self.tbView reloadData];
            
        }else{
            
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
        }
        
    }];
}
- (void)requestData{
    _index = 1;
    NSString *star = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"userinfo/getUserAccount"];
    
    NSDictionary *para = [QParamService GainTransactionDetailsWithStart:star pageSize:@"5"];
    
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [self.dataSource removeAllObjects];
            self.dataSource =  basModel.items;
           
            if(self.dataSource.count == 0){
            
                [QViewUtility showNoRecordViewInView:self.view Title:@"没有任何记录哦~"];
                
            }else{
            
                [self.tbView reloadData];
            }
            
        }else{
        
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
            
        }
        
    }];
}
-(UITableView *)tbView{
    
    if (!_tbView) {
        
        _tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
        _tbView.delegate = self;
        _tbView.dataSource = self;
        _tbView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
    }
    
    return _tbView;
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    MoneyDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MoneyDetailCell"];
    [cell setCellWithData:[self.dataSource objectAtIndex:indexPath.row]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
    
}



ARRAY_INIT(dataSource);

@end
